var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    shopInfo: null,
    type: 0,
    searchValue: '',
    purchaseList: [],
    page: 1,
    limit: 10,
    totalPages: 1,
    hasLogin: false,
    videoContext:null,
    video_data: '',
    fullScreen: false
  },
  init: function () {
    this.setData({
      purchaseList: [],
      page: 1,
      limit: 10,
      totalPages: 1
    });
  },
  switchTab: function (event) {
    let type = event.currentTarget.dataset.index;
    this.setData({
      type: type
    });
    this.init();
    this.getPurchaseList();
  },
  getPurchaseList(reachBottom = false) {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.QuoteList, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit,
      searchValue: that.data.searchValue
    }).then(function (res) {
      if (res.errno === 0) {
        let purchaseList = res.data.list.map(item => {
          item.publishDt = util.formatTime2(new Date(item.publishDt), "Y/M/D");
          item.herbListImage && (item.herbListImage01 = item.herbListImage.split(',')[0]);
          return item;
        });
        //触底加载新数据并保留老数据
        if (reachBottom) {
          purchaseList = [...that.data.purchaseList, ...purchaseList]
        }
        that.setData({
          purchaseList: purchaseList,
          totalPages: res.data.pages
        });

        wx.hideLoading();
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type: options.type
      });
    }
  },
  onReady: function () {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.init();
    this.getPurchaseList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
    } else {
      return false;
    }
    this.getPurchaseList(true);
  },
  onShow: function () {
    if (app.globalData.hasLogin) {
      
      let shopInfo = wx.getStorageSync('shopInfo');
      this.setData({
        shopInfo: shopInfo
      });

      this.init();
      this.getPurchaseList();
    }
    this.setData({
      hasLogin: app.globalData.hasLogin
    });
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },
  //采购明细
  openPurchase(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let purchase = this.data.purchaseList[index];

    wx.setStorageSync('purchasesStr', JSON.stringify([purchase]));
    wx.navigateTo({
      url: `/pages/ucenter/quoteDetail/quoteDetail?id=${purchase.id}&itemId=${purchase.itemId}`
    });
  },
  //查看收据
  openReceipt(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let item = this.data.purchaseList[index];

    wx.navigateTo({
      url: `/pages/ucenter/receiptDetail/receiptDetail?id=${item.id}&itemId=${item.itemId}`
    });
  },
  //按下事件开始  
  touchStart: function (e) {
    let that = this;
    that.setData({
      touchStart: e.timeStamp
    })
  },
  //按下事件结束  
  touchEnd: function (e) {
    let that = this;
    that.setData({
      touchEnd: e.timeStamp
    })
  },
  onChange(e) {
    this.setData({
      searchValue: e.detail,
    });
  },
  onSearch() {
    this.getPurchaseList();
  },
  playVideo(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let purchase = this.data.purchaseList[index];
    this.setData({
      video_data: purchase.herbVideo
    });
    this.play();
  },
  play(e) {
    //执行全屏方法  
    this.videoContext = wx.createVideoContext('myvideo', this);
    this.videoContext.requestFullScreen();
    // this.videoContext.play();
    this.setData({
      fullScreen: true
    })
  },
  /**关闭视屏 */
  closeVideo() {
    //执行退出全屏方法
    this.videoContext.exitFullScreen();
  },
  /**视屏进入、退出全屏 */
  fullScreen(e) {
    var isFull = e.detail.fullScreen;
    //视屏全屏时显示加载video，非全屏时，不显示加载video
    this.setData({
      fullScreen: isFull
    })
  }
})